<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StarsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('stars.index');
    }

    public function get()
    {
        return view('stars.get');
    }
    public function post()
    {
        return view('stars.post');
    }

    public function upload(Request $request)
    {
        if ($request->hasFile('avatar')){
            $file = $request->file('avatar');
            if ($file->isValid()) {
                $originalName = $file->getClientOriginalName();
                // 上传封面到服务器
                $destinationPath = public_path('images/avatars/'.date('Y-m-d').'/');
                if (!is_dir($destinationPath)) {
                    mkdir($destinationPath, 0777, true);
                }
                $file->move($destinationPath, $originalName);
                $filename = '/images/avatars/'.date('Y-m-d').'/'.$originalName;
                return response(['name' =>$originalName, 'url'=> $filename]);
            }
        } else {
            return response()->json(['error' => '未获取图片'], 500);
        }
    }
}
